package fmu.pi.model.connector;

import java.sql.Connection;
import java.sql.SQLException;

import fmu.pi.model.connector.connections.ConexaoHsql;
import fmu.pi.model.connector.connections.ConexaoMysql;

public class ConnectionManager {
	
	private Connection conn;
	private static ConnectionManager manager;
	
	public static final int STANDALONE = 0;
	public static final int SERVER = 1;
	
	private ConnectionManager(){
		
	}
	
	public static ConnectionManager getInstance() {
		if (manager == null) {
			manager = new ConnectionManager();
		}		
		return manager;
	}
	
	public Connection getConnection(int type) throws SQLException {
		
		if (conn == null) {
			switch (type) {
				case STANDALONE : 	conn = new ConexaoHsql().openConnection();  break;
				case SERVER: 		conn = new ConexaoMysql().openConnection(); break;
			}
		}
		return conn;
		
	}
	
	public Connection getConnection() throws SQLException {
		return getConnection(STANDALONE);
	}
	
	
	public void closeConnection() throws SQLException{
		if (conn != null) {
			conn.close();
		}
	}
}
